﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace TaobaoLibrary
{
    public class MicroScopeDataParser
    {
        public static Dictionary<string, string> ParseMicroScopeData(string input)
        {
            Dictionary<string, string> dicData = new Dictionary<string, string>();
            Match matchData = Regex.Match(input, "<meta name=\"microscope-data\" content=\"(.*?)\">");
            string strData = matchData.Groups[1].Value;
            foreach (string strItem in strData.Split(';'))
            {
                if (strItem.IndexOf(',') != -1)
                    //TODO: 处理带逗号的情况
                    continue;

                string[] arrKeyValue = strItem.Split('=');
                arrKeyValue[0] = arrKeyValue[0].Trim();
                arrKeyValue[1] = arrKeyValue[1].Trim();
                if (arrKeyValue.Length == 1)
                {
                    if (!dicData.ContainsKey(arrKeyValue[0]))
                    {
                        dicData.Add(arrKeyValue[0], null);
                    }
                }
                else
                {
                    if (!dicData.ContainsKey(arrKeyValue[0]))
                    {
                        dicData.Add(arrKeyValue[0], arrKeyValue[1]);
                    }
                    else
                    {
                        dicData[arrKeyValue[0]] = arrKeyValue[1];
                    }
                }
            }
            return dicData;
        }
    }
}
